﻿//===================================================================================
// Copyright (c) Ryan Garrett.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===================================================================================

using System;
using System.Collections.Generic;

namespace Caffeine.Core.Test.TestModel
{
    public class ModelForChildVMSequenceTest : ModelBase
    {
        private readonly ModelProperty<String> _stringProperty;
        private readonly ModelProperty<ModelForChildVMSequenceTest> _childProperty;
        private readonly ModelProperty<ModelForChildVMSequenceTest> _childProperty1;
        private readonly ModelProperty<ModelForChildVMSequenceTest> _childProperty2;

        public ModelForChildVMSequenceTest()
            : this(false)
        {
        }

        public static void AddEventToDictionary(IDictionary<String, Int32> eventDictionary, String eventName, Int32 currentChangeCount)
        {
            if (eventDictionary.ContainsKey(eventName))
            {
                eventDictionary[eventName + "2"] = currentChangeCount;
            }
            else
            {
                eventDictionary[eventName] = currentChangeCount;
            }
        }

        public ModelForChildVMSequenceTest(Boolean makeChildProperty1SetIsDirty)
        {
            _stringProperty = RegisterProperty<String>("StringProperty");
            _childProperty = RegisterProperty<ModelForChildVMSequenceTest>("ChildProperty");

            if (makeChildProperty1SetIsDirty)
            {
                _childProperty1 = RegisterProperty<ModelForChildVMSequenceTest>("ChildProperty1");
            }
            else
            {
                _childProperty1 = RegisterProperty<ModelForChildVMSequenceTest>("ChildProperty1", ModelBase.StandardBehaviors.RemoveBehavior(ModelPropertyBehaviors.SetsIsDirty));
            }

            _childProperty2 = RegisterProperty<ModelForChildVMSequenceTest>("ChildProperty2", ModelBase.StandardBehaviors.RemoveBehavior(ModelPropertyBehaviors.SetsIsInvalid));
        }

        protected override void OnInitialized()
        {
            base.OnInitialized();

            ChildProperty1.ValidationMethod = delegate(ModelProperty<ModelForChildVMSequenceTest> property, ref IEnumerable<ValidationError> validationErrors, ref Boolean isValid)
            {
                isValid = !property.IsDirty;
            };

            StringProperty.ValidationMethod = delegate(ModelProperty<String> property, ref IEnumerable<ValidationError> validationErrors, ref Boolean isValid)
            {
                isValid = !property.IsDirty;
            };
        }

        public ModelProperty<String> StringProperty
        {
            get
            {
                return _stringProperty;
            }
        }

        public ModelProperty<ModelForChildVMSequenceTest> ChildProperty
        {
            get
            {
                return _childProperty;
            }
        }

        public ModelProperty<ModelForChildVMSequenceTest> ChildProperty1
        {
            get
            {
                return _childProperty1;
            }
        }

        public ModelProperty<ModelForChildVMSequenceTest> ChildProperty2
        {
            get
            {
                return _childProperty2;
            }
        }

        public Dictionary<String, Int32> OverrideCallCounts
        {
            get;
            set;
        }

        public static Int32 PropertyChangeCount
        {
            get;
            set;
        }

        protected override void OnBeforeParentChanged()
        {
            AddEventToDictionary(OverrideCallCounts, Name + "__" + "OnBeforeParentChanged", PropertyChangeCount++);
            base.OnBeforeParentChanged();
        }

        protected override void OnParentChanged()
        {
            AddEventToDictionary(OverrideCallCounts, Name + "__" + "OnParentChanged", PropertyChangeCount++);
            base.OnParentChanged();
        }

        protected override void OnIsDirtyChanged()
        {
            AddEventToDictionary(OverrideCallCounts, Name + "__" + "OnIsDirtyChanged", PropertyChangeCount++);
            base.OnIsDirtyChanged();
        }

        protected override void OnIsDirtyWithinChanged()
        {
            AddEventToDictionary(OverrideCallCounts, Name + "__" + "OnIsDirtyWithinChanged", PropertyChangeCount++);
            base.OnIsDirtyWithinChanged();
        }

        protected override void OnIsInvalidChanged()
        {
            AddEventToDictionary(OverrideCallCounts, Name + "__" + "OnIsInvalidChanged", PropertyChangeCount++);
            base.OnIsInvalidChanged();
        }

        protected override void OnIsInvalidWithinChanged()
        {
            AddEventToDictionary(OverrideCallCounts, Name + "__" + "OnIsInvalidWithinChanged", PropertyChangeCount++);
            base.OnIsInvalidWithinChanged();
        }

        protected override void OnParentPropertyChanged(ModelScalarPropertyBase parentProperty)
        {
            AddEventToDictionary(OverrideCallCounts, Name + "__" + "OnParentPropertyChanged", PropertyChangeCount++);
            base.OnParentPropertyChanged(parentProperty);
        }

        protected override void OnAssignedToProperty(ModelScalarPropertyBase parentProperty)
        {
            AddEventToDictionary(OverrideCallCounts, Name + "__" + "OnAssignedToProperty", PropertyChangeCount++);
            base.OnAssignedToProperty(parentProperty);
        }

        protected override void OnCommittedToProperty(ModelScalarPropertyBase parentProperty)
        {
            AddEventToDictionary(OverrideCallCounts, Name + "__" + "OnCommittedToProperty", PropertyChangeCount++);
            base.OnCommittedToProperty(parentProperty);
        }

        protected override void OnMarkedForRemovalFromProperty(ModelScalarPropertyBase parentProperty)
        {
            AddEventToDictionary(OverrideCallCounts, Name + "__" + "OnMarkedForRemovalFromProperty", PropertyChangeCount++);
            base.OnMarkedForRemovalFromProperty(parentProperty);
        }

        protected override void OnRemovedFromParentProperty(ModelScalarPropertyBase parentProperty)
        {
            AddEventToDictionary(OverrideCallCounts,Name + "__" + "OnRemovedFromParentProperty", PropertyChangeCount++);
            base.OnRemovedFromParentProperty(parentProperty);
        }

        protected override void OnPropertyIsDefaultChanged(ModelScalarPropertyBase property)
        {
            AddEventToDictionary(OverrideCallCounts,Name + "__" + "OnPropertyIsDefaultChanged", PropertyChangeCount++);
            base.OnPropertyIsDefaultChanged(property);
        }

        protected override void OnPropertyIsDirtyChanged(ModelPropertyBase property)
        {
            AddEventToDictionary(OverrideCallCounts, Name + "__" + "OnPropertyIsDirtyChanged", PropertyChangeCount++);
            base.OnPropertyIsDirtyChanged(property);
        }

        protected override void OnPropertyIsInvalidChanged(ModelPropertyBase property)
        {
            AddEventToDictionary(OverrideCallCounts, Name + "__" + "OnPropertyIsInvalidChanged", PropertyChangeCount++);
            base.OnPropertyIsInvalidChanged(property);
        }

        protected override void OnPropertyValueChanged(ModelScalarPropertyBase property)
        {
            AddEventToDictionary(OverrideCallCounts, Name + "__" + "OnPropertyValueChanged", PropertyChangeCount++);
            base.OnPropertyValueChanged(property);
        }

        protected override void OnPropertyCommittedValueChanged(ModelScalarPropertyBase property)
        {
            AddEventToDictionary(OverrideCallCounts, Name + "__" + "OnPropertyCommittedValueChanged", PropertyChangeCount++);
            base.OnPropertyCommittedValueChanged(property);
        }
    }
}
